﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;

namespace CodeFactory.Controllers
{
    public class Line
    {
        string _l;

        public string Codes
        {
            get { return _l; }
        }

        public Line(string line)
        {
            _l = line;
        }

        public Line AddLine(string line)
        {
            var l = new Line(_l + "\r\n" + line);
            return l;
        }
    }

    public class HomeController : Controller
    {
        //public ActionResult Index()
        //{

        //    ViewBag.Message = "";

        //    return View();
        //}

        public ActionResult Index(string classname)
        {
            if (!string.IsNullOrEmpty(classname))
            {
                string code = GetControllerCode(classname);
                TextWriter tw = new StreamWriter("e:\\codes\\" + classname + ".cs");
                // write a line of text to the file
                tw.WriteLine(code);
                // close the stream
                tw.Close();

            }
            return View();
        }

        private string GetControllerCode(string classname)
        {
            Line code = new Line("using System;");
            code = code.AddLine("using System.Collections.Generic;")
            .AddLine("using System.Data;")
            .AddLine("using System.Data.Entity;")
            .AddLine("using System.Linq;")
            .AddLine("using System.Web;")
            .AddLine("using System.Web.Mvc;")
            .AddLine("using Utilities;")
            .AddLine("namespace ")
            .AddLine("{ ")
            .AddLine(string.Format("public class {0}Controller : Controller", classname))
            .AddLine("{")
            .AddLine("    public ViewResult Index()")
            .AddLine("    {")
            .AddLine(string.Format("        return View(CH.GetAllData<{0}>());", classname))
            .AddLine("    }")
            .AddLine("")
            .AddLine("    public ViewResult Details(int id)")
            .AddLine("    {")
            .AddLine(string.Format("       return View(CH.GetDataById<{0}>(id));", classname))
            .AddLine("    }")
            .AddLine("")
            .AddLine("    public ActionResult Create()")
            .AddLine("    {")
            .AddLine("        return View();")
            .AddLine("    }")
            .AddLine("")
            .AddLine("   [HttpPost]")
            .AddLine(string.Format("   public ActionResult Create({0} item)", classname))
            .AddLine("   {")
            .AddLine("       if (ModelState.IsValid)")
            .AddLine("       {")
            .AddLine(string.Format("             CH.Create<{0}>(item);", classname))
            .AddLine("             return RedirectToAction(\"Index\"); ")
            .AddLine("       }")
            .AddLine("       return View(item);")
            .AddLine("   }")
            .AddLine("   public ActionResult Edit(int id)")
            .AddLine("   {")
            .AddLine(string.Format("       return View(CH.GetDataById<{0}>(id));", classname))
            .AddLine("   }")
            .AddLine("")
            .AddLine("   [HttpPost]")
            .AddLine(string.Format("   public ActionResult Edit({0} item)", classname))
            .AddLine("   {")
            .AddLine("       if (ModelState.IsValid)")
            .AddLine("       {")
            .AddLine(string.Format("            CH.Edit<{0}>(article);", classname))
            .AddLine("            return RedirectToAction(\"Index\");")
            .AddLine("       }")
            .AddLine("       return View(item);")
            .AddLine("   }")
            .AddLine("")
            .AddLine("   public ActionResult Delete(int id)")
            .AddLine("   {")
            .AddLine(string.Format("       return View(CH.GetDataById<{0}>(id));", classname))
            .AddLine("   }")
            .AddLine("")
            .AddLine("   [HttpPost, ActionName(\"Delete\")]")
            .AddLine("   public ActionResult DeleteConfirmed(int id)")
            .AddLine("  {")
            .AddLine(string.Format("      CH.Delete<{0}>(id);", classname))
            .AddLine("      return RedirectToAction(\"Index\");")
            .AddLine("  }")
            .AddLine("}")
            .AddLine("}");

            return code.Codes;
        }

        public ActionResult About()
        {
            return View();
        }
    }
}
